<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/27
 * Time: 11:58
 */

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\NavigationList;
use App\NavigationType;



header('Access-Control-Allow-Origin:*');

class ListController extends Controller{


    public function list(){

        $key = 'nav_index_list';
        $list = Cache::get($key);
        if(!$list){
            $list = NavigationType::with([
                'list' => function($query){
                    $query ->where('state',1)->orderBy('order','desc')->select('id','navigation_type','name','image','description','url','is_login');
                }
                ])->where('state',1)
                ->orderBy('order','desc')
                ->select('id','name')
                ->get()
                ->toArray();

//            Cache::put($key,$list,600);
        }

        foreach ($list as $k=>$v){
            if(!$v['list']){
                unset($list[$k]);
            }elseif (count($v['list']) > 6){
                $list[$k]['list'] = array_slice($v['list'], 0, 6, true);
            }
        }

        $type = array_column($list,'name','id');

        return view('list',['list'=>$list,'typelist'=>$type]);

    }


    public function more($tid){

        $list = NavigationType::with([
            'list' => function($query){
                $query ->where('state',1)->orderBy('order','desc')->select('id','navigation_type','name','image','description','url');
            }
            ])->where('id',$tid)
            ->orderBy('order','desc')
            ->select('id','name')
            ->first()
            ->toArray();

        return view('more',['info'=>$list]);
    }


    //站内搜索
    public function search(){

        if(isset($_POST['t_con']) && $_POST['t_con']){
            $con = $_POST['t_con'];

            $list = NavigationList::where('id',$con)
                    ->orWhere('name','like','%'.$con.'%')
                    ->orWhere('description','like','%'.$con.'%')
                    ->orderBy('order','desc')
                    ->select('id','navigation_type','name','image','description','url')
                    ->get()
                    ->toArray();

        }else{
            $list = [];
        }

        return view('search',['list'=>$list]);
    }




}